package cn.wetcland.service;

import cn.TCSP;
import cn.hutool.crypto.symmetric.AES;
import cn.util.TCUtil;
import cn.wetcland.base.WetclandScoreBoard;
import cn.wetcland.dao.WetclandPlayerDao;
import cn.wetcland.item.SeniorItems;
import cn.wetcland.panel.SignInPanel;
import cn.wetcland.pojo.WetclandPlayer;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.hover.content.Text;
import org.apache.ibatis.session.SqlSession;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.Date;
import java.util.Map;
import java.util.Objects;

public class LoginService {

    public LoginService(Player player, String password) {
        Map<String, Object> map = (Map<String, Object>) TCSP.getPlayerAttr().get(player.getName());
        boolean lr = (boolean) map.get("lr");
        if (player == null || lr) {
            player.sendMessage("§a已登录");
        } else {
            SqlSession sqlSession = TCSP.getSqlSessionFactory().openSession(true);
            WetclandPlayerDao mapper = sqlSession.getMapper(WetclandPlayerDao.class);
            WetclandPlayer one = mapper.findOne(player.getName());
            if (one == null) {
                player.sendMessage("§c登录发生错误, 请重试");
                sqlSession.close();
                return;
            }
            AES aes = new AES("taniloveyouandey".getBytes());
            String s = aes.decryptStr(one.getPassword());
            if (s.equals(password)) {
                map.put("lr", true);
                player.sendMessage("§a登录成功");
                player.setGameMode(GameMode.SURVIVAL);
                // 登录成功自动打开签到面板
                player.openInventory(SignInPanel.getPlayerSignInPanel(player));
                BaseComponent[] baseComponents = new ComponentBuilder().append("§d打开聊天栏用鼠标点击此处 http://www.wetcland.cn 进入官网")
                        .event(new ClickEvent(ClickEvent.Action.OPEN_URL, "http://www.wetcland.cn"))
                        .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text("§a点击进入Wetcland官网")))
                        .create();
                if (one.getEmail() == null || one.getEmail().trim().equals("")) {
                    player.sendMessage("§c——————————重要通知———————————");
                    player.sendMessage("§c 检测到您没有绑定邮箱");
                    player.sendMessage("§c 请到官网绑定邮箱, 以防密码遗失");
                    player.sendMessage("§c——————————————————————————————");
                    player.spigot().sendMessage(baseComponents);
                }
                if (one.getPhone() == null || one.getPhone().trim().equals("")) {
                    player.sendMessage("§c——————————重要通知———————————");
                    player.sendMessage("§c 检测到您没有完善个人基本信息");
                    player.sendMessage("§c 请到官网完善信息, 以获得更好游戏体验");
                    player.sendMessage("§c——————————————————————————————");
                    player.spigot().sendMessage(baseComponents);
                }
                if (one.getLastLogin() == null) {
                    // 给予玩家小助手
                    player.getInventory().addItem(SeniorItems.mainMenuItem());
                    // 给予新手礼包
                    player.getInventory().addItem(new ItemStack(Material.DIAMOND_SWORD),
                            new ItemStack(Material.DIAMOND_HELMET), new ItemStack(Material.DIAMOND_CHESTPLATE),
                            new ItemStack(Material.DIAMOND_BOOTS), new ItemStack(Material.DIAMOND_LEGGINGS),
                            new ItemStack(Material.FIREWORK_ROCKET, 6));
                    player.playSound(player.getLocation(), "wetcland.bgm_rain", 5f, 1f);
                }
                WetclandScoreBoard wetclandScoreBoard = new WetclandScoreBoard(player);
                wetclandScoreBoard.startShowing();
                map.put("wsb", wetclandScoreBoard);
                WetclandPlayer attrPlayer = TCUtil.getAttrPlayer(player.getName());
                attrPlayer.setLastLogin(new Date());
                attrPlayer.setIp(Objects.requireNonNull(player.getAddress()).getAddress().getHostAddress());
                // 验证VIP过期时间
                if (attrPlayer.getMemberType() != 0 && attrPlayer.getMemberEndDate() != null && attrPlayer.getMemberEndDate().before(new Date())) {
                    // 将VIP取消
                    attrPlayer.setMemberType(0);
                    player.sendMessage("§c您的VIP会员已过期");
                }
                // 更新玩家
                mapper.updatePlayer(attrPlayer);
                String memberTypeString = TCUtil.getMemberTypeString(attrPlayer.getMemberType());
                if (memberTypeString != null) {
                    Bukkit.broadcastMessage("§d♛ ♛ ♛ ♛ ♛ ♛ ♛ ♛会员专属入场♛ ♛ ♛ ♛ ♛ ♛ ♛ ♛");
                    Bukkit.broadcastMessage("§e" + memberTypeString + " 会员§d"+player.getName() + "§b登陆了游戏");
                    Bukkit.broadcastMessage("§d₯ ₯₯ ₯₯₯₯₯₯₯₯₯₯₯₯₯₯₯₯₯₯₯₯₯₯₯₯₯₯₯₯₯₯₯₯₯₯ ₯₯ ₯");
                }
            } else {
                player.sendMessage("§c密码错误");
            }
            sqlSession.close();
        }
    }

}
